import userMenu from './template/userMenu.vue';
import myHouse from './template/myHouse.vue';

import footNav from '@/components/footerNav.vue';
import { mapState,mapMutations } from 'Vuex';

import {MessageBox} from 'mint-ui';
import {reLoginWX} from '@/util/util.js';
export default {
  name: "members",
  data: () => ({
    userInfo:'',
    isLogin:false,
  }),
  components: {
    userMenu,
    myHouse,
    footNav
  },
  computed:{
    ...mapState({
      relogin:state=>state.login.relogin,
      userMessage:state=>state.login.userInfo,
    }),

  },
  mounted() {
    //do something after mounting vue instance
    let that = this;
    let token = window.localStorage.getItem('userToken');
    if(token=='null' || token==undefined){
      console.log('您未登录');
      MessageBox.alert('您还未登录，请先登录?').then(action => {
        that.$router.push({ name:'login'});
      });
    }
    this.$nextTick( () => {
      console.log(that.relogin);
      if (that.relogin) {
        window.location.reload();
      }else{
        network.get(configUrl.QUERYUSER_URL,'',function(res){
          console.log(res);
          if(res.code==401){
            MessageBox.alert(res.message).then(action => {
                // that.$router.push({name:'login'});
                reLoginWX();
              }).catch(err=>{
                console.log(err);
              });
          }else{
            that.isLogin = true;
            that.userInfo = res.data;
            that.setUserInfo(res.data);
            window.localStorage.setItem('userInfo',JSON.stringify(res.data));
          }
        },function(err){
          console.log(err);
        });
      }

    });
  },
  methods: {
    ...mapMutations([
      'setUserInfo',
    ]),
    toLogin() {   //跳转去登录
      this.$router.push({path:'/login'});
    },
    toMemberset() {   //个人信息设置
      if (this.isLogin) {
        this.$router.push({path:'/memberSet'});
      }else {
        this.$router.push({path:'/login'});
      }
    },
    callService() {
      // 客服电话
      network.get(configUrl.SERVERPHONE_URL,'',function (res) {
        // console.log(res);
        var servicePhone = res.data.servicePhone;
        window.location.href = 'tel://'+servicePhone;
      },function (error) {
        console.log(error);
      });
    },
    // 意见反馈
    toFeedback() {
      this.$router.push({name:'feedback'});
    },
    // 退出登录
    quitLogin() {
      MessageBox.confirm('确定退出本次登录?').then(action => {
        window.localStorage.removeItem('userToken');    //清除缓存对应缓存
        this.$router.push({path:'/login'});
        // window.location.reload();      //刷新页面
      }).catch(err => {
        console.log(err);
      })
    },

  }
}
